from sanic import Sanic
import aiomysql
import aioredis
from sanic import response
import asyncio
from product_api import SpecificMethoD

SpecificMethod = SpecificMethoD()
import json

from . import bp_product
@bp_product.route("/product_guanxi",methods=['POST'])
async def product_type_info(request):

    if not request.method == 'POST':
    
        return response.json({
                'status': -1,
                'errorCode': 1,
                'errorMsg':f"请求方式error,request.method:{request.method}",
                'resultBody':None
            })

    info = request.form

    product_id = info.get('product_id')

    product_type_id = info.get('product_type_id')

    if not all([product_id,product_type_id]):
    
        return response.json({
                'status': -1,
                'errorCode': 3,
                'errorMsg':f"缺少参数product_id:{product_id},product_type_id:{product_type_id}",
                'resultBody':None
            })

    is_product_work = await SpecificMethod.select_info(sql="select * from product where id=%s",args=product_id)
    print(is_product_work)
    if is_product_work == ():
        return response.json({
                            'status': -1,
                            'errorCode': 4,
                            'errorMsg':f'请检查参数是否存在product_id{product_id}',
                            'resultBody':None
                        })

    is_producttype_work = await SpecificMethod.select_info(sql="select * from product_type where id=%s",args=product_type_id)

    if is_producttype_work == ():
        return response.json({
                            'status': -1,
                            'errorCode': 5,
                            'errorMsg':f'请检查参数是否存在product_type_id{product_type_id}',
                            'resultBody':None
                        })

    is_guanxi_work = await SpecificMethod.select_info(sql="select * from intermediate_table where product_id=%s and product_type_id=%s",args=(product_id,product_type_id))
    # print(is_guanxi_work)
    if not is_guanxi_work == ():
        return response.json({
                            'status': -1,
                            'errorCode': 6,
                            'errorMsg':f'请检查参数是否已存在product_type_id{product_type_id}，product_id{product_id}',
                            'resultBody':None
                        })

    work = SpecificMethod.insert_info(sql="insert into intermediate_table(product_id,product_type_id)values(%s,%s);",args=(product_id,product_type_id))

    if not work:

        return response.json({
                                'status': -1,
                                'errorCode': 7,
                                'errorMsg':f'请检查参数格式product_type_id：{product_type_id}，product_id：{product_id}',
                                'resultBody':None
                            })

    return response.json({
                'status': 1,
                'errorCode': None,
                'errorMsg':None,
                'resultBody':'创建成功'
            })

